﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;

namespace SeguradoraIA.Framework
{
    public class Security
    {
        #region - Enum - 

        /// <summary>  
        /// Niveis de criptografia AES  
        /// Criptografia de 128, 192 e 256 Bits.  
        /// </summary>  
        public enum AESCryptographyLevel : int
        {
            /// <summary>  
            /// Criptografia AES 128 Bits  
            /// </summary>  
            AES_128 = 128,

            /// <summary> 
            /// Criptografia AES 192 Bits  
            /// </summary>  
            AES_192 = 192,

            /// <summary>  
            /// Criptografia AES 256 Bits  
            /// </summary>  
            AES_256 = 256
        }
        #endregion

        #region - Encriptografar - 
        /// <summary>  
        /// Criptografa usando Rijndael (128,192,256 Bits).  
        /// </summary>  
        /// <param name="data">valor a ser criptografado</param>  
        /// <param name="password">senha de criptografia</param>  
        /// <param name="bits">Nível de criptografia (128,192,256 bits)</param>  
        /// <returns>string criptografada</returns>  
        public static string EncryptSAH2(string data)
        {
            try
            {
                byte[] clearBytes = Encoding.Unicode.GetBytes(data);
                PasswordDeriveBytes pdb = new PasswordDeriveBytes("SeguradoraIA",
                    new byte[] { 0x00, 0x01, 0x02, 0x1C, 0x1D, 0x1E, 0x03, 0x04, 0x05, 0x0F, 0x20, 0x21, 0xAD, 0xAF, 0xA4 });

                byte[] encryptedData = Encrypt(clearBytes, pdb.GetBytes(32), pdb.GetBytes(16));
                return Convert.ToBase64String(encryptedData);
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        private static byte[] Encrypt(byte[] clearData, byte[] Key, byte[] IV)
        {
            MemoryStream ms = new MemoryStream();
            Rijndael alg = Rijndael.Create();

            alg.Key = Key;
            alg.IV = IV;

            CryptoStream cs = new CryptoStream(ms, alg.CreateEncryptor(), CryptoStreamMode.Write);

            cs.Write(clearData, 0, clearData.Length);
            cs.Close();

            byte[] encryptedData = ms.ToArray();

            return encryptedData;
        }
        #endregion

        #region - Descriptografar - 
        /// <summary>  
        /// Descriptografa string usando Rijndael (128,192,256 Bits).  
        /// </summary>  
        /// <param name="data">valor a ser descriptografado</param>  
        /// <param name="password">senha de descriptografia</param>  
        /// <param name="bits">Nível de descriptografia (128,192,256 bits)</param>  
        /// <returns>string descriptografada</returns>  
        public static string DecryptSAH2(string data)
        {
            try
            {
                byte[] cipherBytes = Convert.FromBase64String(data);

                PasswordDeriveBytes pdb = new PasswordDeriveBytes("SeguradoraIA",
                    new byte[] { 0x00, 0x01, 0x02, 0x1C, 0x1D, 0x1E, 0x03, 0x04, 0x05, 0x0F, 0x20, 0x21, 0xAD, 0xAF, 0xA4 });

                byte[] decryptedData = Decrypt(cipherBytes, pdb.GetBytes(32), pdb.GetBytes(16));
                return Encoding.Unicode.GetString(decryptedData);
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        private static byte[] Decrypt(byte[] cipherData, byte[] Key, byte[] IV)
        {
            MemoryStream ms = new MemoryStream();
            Rijndael alg = Rijndael.Create();

            alg.Key = Key;
            alg.IV = IV;

            CryptoStream cs = new CryptoStream(ms, alg.CreateDecryptor(), CryptoStreamMode.Write);

            cs.Write(cipherData, 0, cipherData.Length);
            cs.Close();

            byte[] decryptedData = ms.ToArray();

            return decryptedData;
        }
        #endregion
    }
}
